package de.breitbeil.jbooking.ui.form;

import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JSpinner;

import org.springframework.richclient.command.config.CommandConfigurer;
import org.springframework.richclient.form.AbstractForm;
import org.springframework.richclient.form.binding.swing.SwingBindingFactory;
import org.springframework.richclient.form.builder.TableFormBuilder;

import de.breitbeil.jbooking.command.ConnectionTestCommand;

public class FtpCalendarForm extends AbstractForm {

    public static final String ID = "ftpCalendarForm";
    
    public FtpCalendarForm(FtpCalendarFormObject formObject) {
        super(formObject);
        setId(ID);
    }
    
    @Override
    protected JComponent createFormControl() {
        SwingBindingFactory bindingFactory = (SwingBindingFactory) getBindingFactory();
        TableFormBuilder formBuilder = new TableFormBuilder(bindingFactory);
        formBuilder.setLabelAttributes("colGrId=label colSpec=right:pref");
        
        JLabel statusLabel = new JLabel();
        AbstractButton testConnectionButton = createTestConnectionButton(statusLabel); 
        
        formBuilder.addSeparator(getMessage(ID+".ftpSeparator.label"));
        formBuilder.row();
        formBuilder.add("host");
        formBuilder.add("port", "colSpec=2cm");
        formBuilder.row();
        formBuilder.add("username");
        formBuilder.row();
        formBuilder.addPasswordField("password");
        formBuilder.row();
        formBuilder.getLayoutBuilder().cell();
        formBuilder.getLayoutBuilder().cell(statusLabel);
        //formBuilder.row();
        formBuilder.getLayoutBuilder().cell(testConnectionButton);
        formBuilder.row();
        
        formBuilder.addSeparator(getMessage(ID+".dataSeparator.label"));
        formBuilder.row();
        formBuilder.add("remoteFile");
        formBuilder.row();
        formBuilder.add(bindingFactory.createBoundComboBox("suffix", getAvailableSuffix()));
        formBuilder.row();
        formBuilder.add("firstDisplayedDay");
        
        return formBuilder.getForm();
    }
    
    protected AbstractButton createTestConnectionButton(JLabel statusLabel) {
        ConnectionTestCommand connectionTestCommand = new ConnectionTestCommand(getFormModel(), statusLabel);
        CommandConfigurer commandConfigurer = getCommandConfigurer();
        commandConfigurer.configure(connectionTestCommand);
        
        return connectionTestCommand.createButton(); 
    }
    
    private String[] getAvailableSuffix() {
        return ImageIO.getWriterFileSuffixes();
    }

}
